var autoDJ = false;
var minPlaylistLength = 1;

function toggleAutoDJ() {
	autoDJ = !autoDJ
	setAutoDJButton(autoDJ);
	
	// no songs loaded, but playlist have songs, start playing!
	if (autoDJ && vidState[0] == -1 && vidState[1] == -1 && playlistGetLength() > 0) {
		playlistPlayNext(currentSlot, true);
	}
}

function djPlay() {
	// set active slot
	setActiveSlot(currentSlot);
	// playing?
	videoPlay(currentSlot);
}

function djPause() {
	videoPause(currentSlot);
}

function djNext() {
	// state:  next vid already playing
	// action: switch focus
	if (vidState[otherSlot(currentSlot)] == 1) {
		//do nothing, changing focus at end of function
		printLog('Already playing..');
	// state:  other slot not cued
	// action: load next vid	
	} else if (vidState[otherSlot(currentSlot)] != 2 && vidState[otherSlot(currentSlot)] != 3) {
		if (!playlistPlayNext(otherSlot(currentSlot), true)) {
			printMsg("Playlist is empty...", 1);
			return;
		}
	// state:  other slot cued
	// action: play it!
	} else if (vidState[otherSlot(currentSlot)] == 2) {
		videoPlay(otherSlot(currentSlot));
	} else {
		return;
	}
	// fade and stop current vid
	fadeTo(otherSlot(currentSlot));
	videoStop(otherSlot(currentSlot));
}

function djCheck() {
	
	// states:
	// -1: Unstarted
	// 	0: Ended
	//	1: Playing
	// 	2: Paused
	// 	3: Buffering
	// 	5: Cued
	// Added by Joram
	//  6: Seeking
	
	// check for end of song
	endOfSong = vid[currentSlot].getDuration() - vid[currentSlot].getCurrentTime() < fadingAt;
	
	// state:  current slot playing, other not loaded or ended
	// action: load next vid
	if (vidState[currentSlot] == 1 && (vidState[otherSlot(currentSlot)] == -1 || vidState[otherSlot(currentSlot)] == 0)) {
		printLog("AutoDJ: Load vid from playlist");
		if (!playlistCueNext(otherSlot(currentSlot), true)) {
			loadEntryFromLFM();
		}
	}
	
	// state:  current slot playing, other cued, end of song reached
	// action: play cued slot!
	if (vidState[currentSlot] == 1 && vidState[otherSlot(currentSlot)] == 2 && endOfSong) {
		printLog("AutoDJ: End of song, play cued vid");
		videoPlay(otherSlot(currentSlot));
	}
	
	// state:  both slots playing at end of song
	// action: fade them!
	if (vidState[currentSlot] == 1 && vidState[otherSlot(currentSlot)] == 1 && endOfSong) {
		printLog("AutoDJ: End of song, fade to next vid");
		djFade();
	}
	
	// state:  current slot ended
	// action: full fade to faded in vid, cue next in faded out slot, switch current slot
	if (vidState[currentSlot] == 0 && vidState[otherSlot(currentSlot)] == 1 && endOfSong) {
		printLog("AutoDJ: Song ended, end fade, switch active vid and cue next");
		djFadeEnd();
		//don't set this, because it is automatically done when fading to 0 or 100%, which happens in function djFadeEnd
		//setActiveSlot(otherSlot(autoDJ_currentSlot));
		playlistCueNext(otherSlot(currentSlot), true);
	}
	
}

function djFade() {
	// fading...
	percFade = (vid[currentSlot].getDuration() - vid[currentSlot].getCurrentTime()) / fadingAt * 100;
	if (currentSlot==0) { percFade = 100-percFade; }
	setFader(parseInt(percFade));
	setFaderSlider(parseInt(percFade));
}

function djFadeEnd() {
	percFade = 0;
	if (currentSlot==0) { percFade = 100-percFade; }
	setFader(percFade);
	setFaderSlider(percFade);
}